package com.dmap.analysispoint.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dmap.analysispoint.constants.BurialPointConstant;
import com.dmap.analysispoint.service.DataPreparationService;
import com.dmap.base.send.email.MailService;
import com.dmap.base.units.DateUtil;
import com.dmap.schedule.annotation.ElasticJobConf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import javax.mail.MessagingException;

/**
 * @author guanming
 * @date 18-7-13下午5:32
 * @Description: 按小时对数据看板模块手机、设备数据定时自动分析预处理
 */
@ElasticJobConf(name="按小时对数据看板模块手机、设备数据定时自动分析预处理", cron="0 50 * * * ?", eventTraceRdbDataSource="datasourcelog")
public class CountPhoneEquipmentNumAndAddCacheJob implements SimpleJob {

    private final static Logger logger = LoggerFactory.getLogger(CountPhoneEquipmentNumAndAddCacheJob.class);

    @Value("${spring.profiles.active}")
    private String environment;

    @Resource
    private DataPreparationService dataPreparationService;


    @Override
    public void execute(ShardingContext shardingContext) {
        long startTime = DateUtil.getToday0();
        long endTime = DateUtil.getHourOfMillis() - 1;
        try {
            dataPreparationService.countPhoneEquipmentNumAndAddCache(startTime, endTime);
            MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对数据看板信息定时自动分析预处理定时任务 对数据看板手机、设备数据预处理定时任务成功");
        } catch (MessagingException me) {
            logger.error("按小时对数据看板信息定时自动分析预处理定时任务 对数据看板手机、设备数据预处理定时任务 MailService.sendMail error:  {}", me);
        } catch (Exception e) {
            try {
                MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对数据看板信息定时自动分析预处理定时任务 对数据看板手机、设备数据预处理定时任务 error: " + e);
            } catch (MessagingException me) {
                logger.error("按小时对数据看板信息定时自动分析预处理定时任务 对数据看板手机、设备数据预处理定时任务 MailService.sendMail error:  {}", me);
            }
        }
    }
}
